package main

import (
	"database/sql"
	"fmt"

	_ "github.com/go-sql-driver/mysql"
)

func main() {
	db, err := sql.Open("mysql", "root:root@tcp(localhost:3306)/go_demo")
	if err != nil {
		// 处理连接错误
		panic(err.Error())
	}
	defer db.Close()

	// rows, err := db.Query("SELECT * FROM users")
	// fmt.Println(rows.ColumnTypes())
	// fmt.Println(rows.Columns())
	// if err != nil {
	// 	panic(err.Error())
	// }
	// defer rows.Close()

	// for rows.Next() {
	// 	var id int
	// 	var name string
	// 	var age int
	// 	var email string
	// 	err := rows.Scan(&id, &name, &age, &email)
	// 	if err != nil {
	// 		panic(err.Error())
	// 	}
	// 	fmt.Printf("ID: %d, Name: %s\n", id, name)
	// }
	var uid = 2
	var id int = 0
	var name string = ""
	var age int = 0
	var email string = ""
	err = db.QueryRow("select * from users where uid= ?", uid).Scan(&id, &name, &age, &email)
	if err != nil {
		panic(err.Error())
	}
	fmt.Printf("ID: %d, Name: %s\n", id, name)
}
